package com.yy.bolg.controller.index;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.CustomerBean;
import com.yy.bolg.entity.ElectricianBean;
import com.yy.bolg.entity.FeedbackBean;
import com.yy.bolg.request.CustomerRequest;
import com.yy.bolg.resp.CustomerStatisticsResp;
import com.yy.bolg.resp.ElectricianResp;
import com.yy.bolg.resp.FeedbackResp;
import com.yy.bolg.resp.SystemStatisticsResp;
import com.yy.bolg.service.CustomerService;
import com.yy.bolg.service.ElectricianService;
import com.yy.bolg.service.FeedbackService;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import com.yy.bolg.util.hikfirecloud.HikFireCloud;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页数据统计相关控制器
 * @author enmaoFu
 * @date 2024-12-13
 * RequestMapping("/index") 是说明这个类的访问地址是 /index
 */
@Api(tags = {"首页数据统计相关"})
@Controller
@RequestMapping(value = "/index")
public class IndexController {

    @Autowired
    private CustomerService customerService;// 自动载入Service对象

    @Autowired
    private ElectricianService electricianService;// 自动载入Service对象

    @Autowired
    private FeedbackService feedbackService;// 自动载入Service对象

    /**
     * 业主端小程序首页顶部数据统计
     * 服务器访问路径：http://localhost:8080/bolg/index/customer/statistics
     * 本地访问路径：http://localhost:8111/index/customer/statistics
     * 参数：{"customerCode":"CUS20241102102456"}
     */
    @ApiOperation(value = "业主端小程序首页顶部数据统计", notes = "参数：{\"customerCode\":\"CUS20241102102456\"}", response = CustomerStatisticsResp.class)
    @RequestMapping(value = "/customer/statistics",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    @Deprecated
    public String customerStatistics(@RequestBody CustomerRequest request){
        String result;

        CustomerBean customerBean = new CustomerBean();
        customerBean.setPsCustomerCode(request.getCustomerCode());

        try {
            CustomerStatisticsResp resp = new CustomerStatisticsResp();

            CustomerBean returnCustomerBean = customerService.findOneByCode(customerBean);
            if(null != returnCustomerBean){
                HikFireCloud hikFireCloud = new HikFireCloud();

                Map<String, String> getDeviceListParamMap = new HashMap<>();
                getDeviceListParamMap.put("flagId", "");
                getDeviceListParamMap.put("offset", "0");
                getDeviceListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getDeviceListParamMap.put("pageNo", "1");
                getDeviceListParamMap.put("pageSize", "500");
                String returnStr = hikFireCloud.getDeviceList(getDeviceListParamMap);
                JSONObject DeviceListJson = JSON.parseObject(hikFireCloud.getDeviceList(getDeviceListParamMap));
                JSONObject DeviceListDataJson = JSON.parseObject(DeviceListJson.get("data").toString());

                Map<String, String> getSensorListParamMap = new HashMap<>();
                getSensorListParamMap.put("flagId", "");
                getSensorListParamMap.put("offset", "0");
                getDeviceListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getSensorListParamMap.put("pageNo", "1");
                getSensorListParamMap.put("pageSize", "500");
                JSONObject SensorListJson = JSON.parseObject(hikFireCloud.getSensorList(getSensorListParamMap));
                JSONObject SensorListDataJson = JSON.parseObject(SensorListJson.get("data").toString());

                Map<String, String> getFireDeviceStatusListParamMap = new HashMap<>();
                getFireDeviceStatusListParamMap.put("flagId", "");
                getFireDeviceStatusListParamMap.put("offset", "0");
                getFireDeviceStatusListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getFireDeviceStatusListParamMap.put("pageNo", "1");
                getFireDeviceStatusListParamMap.put("pageSize", "500");
                JSONObject FireDeviceStatusListJson = JSON.parseObject(hikFireCloud.getFireDeviceStatusList(getSensorListParamMap));
                JSONObject FireDeviceStatusListDataJson = JSON.parseObject(FireDeviceStatusListJson.get("data").toString());
                JSONArray FireDeviceStatusListDataListJson = JSON.parseArray(FireDeviceStatusListDataJson.get("list").toString());
                // 在线数量
                int onLineNum = 0;
                // 离线数量
                int offLineNum = 0;
                for(int i = 0; i < FireDeviceStatusListDataListJson.size(); i++){
                    JSONObject job = JSON.parseObject(FireDeviceStatusListDataListJson.get(i).toString());
                    if(job.get("deviceStatus").equals("0")){
                        offLineNum += 1;
                    }else if(job.get("deviceStatus").equals("1")){
                        onLineNum += 1;
                    }
                }

                FeedbackBean feedbackBean = new FeedbackBean();
                feedbackBean.setPsCustomerCode(request.getCustomerCode());
                feedbackBean.setPsFeedbackType(3);
                feedbackBean.setPsFeedbackState(-1);
                feedbackBean.setPsFeedbackClassification(3);
                // 报修数量
                int faultNum = 0;
                // 预警数量
                int warningNum = 0;
                for(FeedbackBean returnfeedbackBean : feedbackService.findAllByCondition(feedbackBean)){
                    if(returnfeedbackBean.getPsFeedbackType() == 1){
                        warningNum += 1;
                    }else{
                        faultNum += 1;
                    }
                }
                resp.setDeviceNum(Integer.parseInt(DeviceListDataJson.get("total").toString()));
                resp.setSensorNum(Integer.parseInt(SensorListDataJson.get("total").toString()));
                resp.setOnLineNum(onLineNum);
                resp.setOffLineNum(offLineNum);
                resp.setFaultNum(faultNum);
                resp.setWarningNum(warningNum);

                Map map = new HashMap();
                map.put("data", resp);

                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
            }else{
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "没有该业主单位",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 业主端小程序首页顶部数据统计-1228版
     * 服务器访问路径：http://localhost:8080/bolg/index/customer/new/statistics
     * 本地访问路径：http://localhost:8111/index/customer/new/statistics
     * 参数：{"customerCode":"CUS20241102102456"}
     */
    @ApiOperation(value = "业主端小程序首页顶部数据统计-1228版", notes = "参数：{\"customerCode\":\"CUS20241102102456\"}", response = CustomerStatisticsResp.class)
    @RequestMapping(value = "/customer/new/statistics",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    @Deprecated
    public String customerNewStatistics(@RequestBody CustomerRequest request){
        String result;

        CustomerBean customerBean = new CustomerBean();
        customerBean.setPsCustomerCode(request.getCustomerCode());

        try {
            CustomerBean returnCustomerBean = customerService.findOneByCode(customerBean);

            if(null != returnCustomerBean) {

                HikFireCloud hikFireCloud = new HikFireCloud();

                Map<String, String> getDeviceListParamMap = new HashMap<>();
                getDeviceListParamMap.put("flagId", "");
                getDeviceListParamMap.put("offset", "0");
                getDeviceListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getDeviceListParamMap.put("pageNo", "1");
                getDeviceListParamMap.put("pageSize", "500");
                JSONObject DeviceListJson = JSON.parseObject(hikFireCloud.getDeviceList(getDeviceListParamMap));
                JSONObject DeviceListDataJson = JSON.parseObject(DeviceListJson.get("data").toString());
                JSONArray DeviceListDataListJson = JSON.parseArray(DeviceListDataJson.get("list").toString());
                // 正常数量
                int onNormalNum = 0;
                // 故障数量
                int onFaultNum = 0;
                for (int i = 0; i < DeviceListDataListJson.size(); i++) {
                    JSONObject job = JSON.parseObject(DeviceListDataListJson.get(i).toString());
                    if (job.get("unitStatus").equals("1")) {
                        onNormalNum += 1;
                    } else if (job.get("unitStatus").equals("0")) {
                        onFaultNum += 1;
                    }
                }

                Map<String, String> getSensorListParamMap = new HashMap<>();
                getSensorListParamMap.put("flagId", "");
                getSensorListParamMap.put("offset", "0");
                getDeviceListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getSensorListParamMap.put("pageNo", "1");
                getSensorListParamMap.put("pageSize", "500");
                JSONObject SensorListJson = JSON.parseObject(hikFireCloud.getSensorList(getSensorListParamMap));
                JSONObject SensorListDataJson = JSON.parseObject(SensorListJson.get("data").toString());

                Map<String, String> getFireDeviceStatusListParamMap = new HashMap<>();
                getFireDeviceStatusListParamMap.put("flagId", "");
                getFireDeviceStatusListParamMap.put("offset", "0");
                getFireDeviceStatusListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getFireDeviceStatusListParamMap.put("pageNo", "1");
                getFireDeviceStatusListParamMap.put("pageSize", "500");
                JSONObject FireDeviceStatusListJson = JSON.parseObject(hikFireCloud.getFireDeviceStatusList(getFireDeviceStatusListParamMap));
                JSONObject FireDeviceStatusListDataJson = JSON.parseObject(FireDeviceStatusListJson.get("data").toString());
                JSONArray FireDeviceStatusListDataListJson = JSON.parseArray(FireDeviceStatusListDataJson.get("list").toString());
                // 在线数量
                int onLineNum = 0;
                // 离线数量
                int offLineNum = 0;
                for (int i = 0; i < FireDeviceStatusListDataListJson.size(); i++) {
                    JSONObject job = JSON.parseObject(FireDeviceStatusListDataListJson.get(i).toString());
                    if (job.get("deviceStatus").equals("0")) {
                        offLineNum += 1;
                    } else if (job.get("deviceStatus").equals("1")) {
                        onLineNum += 1;
                    }
                }

                FeedbackBean feedbackBean = new FeedbackBean();
                feedbackBean.setPsCustomerCode(request.getCustomerCode());
                feedbackBean.setPsFeedbackType(3);
                feedbackBean.setPsFeedbackState(-1);
                feedbackBean.setPsFeedbackClassification(3);
                // 报修数量
                int faultNum = 0;
                // 预警数量
                int warningNum = 0;
                for (FeedbackBean returnfeedbackBean : feedbackService.findAllByCondition(feedbackBean)) {
                    if (returnfeedbackBean.getPsFeedbackType() == 1) {
                        warningNum += 1;
                    } else {
                        faultNum += 1;
                    }
                }

                CustomerStatisticsResp resp = new CustomerStatisticsResp();
                resp.setDeviceNum(Integer.parseInt(DeviceListDataJson.get("total").toString()));
                resp.setSensorNum(Integer.parseInt(SensorListDataJson.get("total").toString()));
                resp.setOnLineNum(onLineNum);
                resp.setOffLineNum(offLineNum);
                resp.setFaultNum(faultNum);
                resp.setWarningNum(warningNum);
                resp.setOnNormalNum(onNormalNum);
                resp.setOnFaultNum(onFaultNum);
                resp.setCustomerName(returnCustomerBean.getPsCustomerName());

                Map map = new HashMap();
                map.put("data", resp);

                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
            }else{
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "没有该业主单位",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 管理系统首页顶部数据统计
     * 服务器访问路径：http://localhost:8080/bolg/index/system/statistics
     * 本地访问路径：http://localhost:8111/index/system/statistics
     */
    @ApiOperation(value = "管理系统首页顶部数据统计", notes = "参数：", response = CustomerStatisticsResp.class)
    @RequestMapping(value = "/system/statistics",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    @Deprecated
    public String systemStatistics(){
        String result;

        try {
            int customerThroughNum = 0;
            int customerWaitNum = 0;
            int customerFailNum = 0;

            int electricianThroughNum = 0;
            int electricianWaitNum = 0;
            int electricianFailNum = 0;
            int electricianBanNum = 0;

            int allWaringProcessedNum = 0;
            int allWaringUntreatedNum = 0;

            int allFaultProcessedNum = 0;
            int allFaultUntreatedNum = 0;

            SystemStatisticsResp resp = new SystemStatisticsResp();

            List<CustomerBean> returnCustomerBeans = customerService.findAll();
            List<ElectricianBean> returnElectricianBeans = electricianService.findAll();
            List<FeedbackBean> returnAllWaringBeans = feedbackService.findAllByHikWaring();
            List<FeedbackBean> returnAllHikBeans = feedbackService.findAllByHikFault();

            if(null != returnCustomerBeans && returnCustomerBeans.size() != 0){
                for(CustomerBean cb: returnCustomerBeans){
                    if(cb.getPsCustomerState() == 1){
                        customerWaitNum += 1;
                    }else if(cb.getPsCustomerState() == 2){
                        customerThroughNum += 1;
                    }else if(cb.getPsCustomerState() == 3){
                        customerFailNum += 1;
                    }
                }
                resp.setCustomerNum(returnCustomerBeans.size());
                resp.setCustomerThroughNum(customerThroughNum);
                resp.setCustomerWaitNum(customerWaitNum);
                resp.setCustomerFailNum(customerFailNum);
            }else {
                resp.setCustomerNum(0);
                resp.setCustomerThroughNum(0);
                resp.setCustomerWaitNum(0);
                resp.setCustomerFailNum(0);
            }

            if(null != returnElectricianBeans && returnElectricianBeans.size() != 0){
                for(ElectricianBean eb: returnElectricianBeans){
                    if(eb.getPsElectricianState() == 1){
                        electricianWaitNum += 1;
                    }else if(eb.getPsElectricianState() == 2){
                        electricianThroughNum += 1;
                    }else if(eb.getPsElectricianState() == 3){
                        electricianFailNum += 1;
                    }else if(eb.getPsElectricianState() == 4){
                        electricianBanNum += 1;
                    }
                }
                resp.setElectricianNum(returnElectricianBeans.size());
                resp.setElectricianThroughNum(electricianThroughNum);
                resp.setElectricianWaitNum(electricianWaitNum);
                resp.setElectricianFailNum(electricianFailNum);
                resp.setElectricianBanNum(electricianBanNum);
            }else {
                resp.setElectricianNum(0);
                resp.setElectricianThroughNum(0);
                resp.setElectricianWaitNum(0);
                resp.setElectricianFailNum(0);
                resp.setElectricianBanNum(0);
            }

            if(null != returnAllWaringBeans && returnAllWaringBeans.size() != 0){
                for(FeedbackBean fb: returnAllWaringBeans){
                    if(fb.getPsFeedbackState() == 1){
                        allWaringUntreatedNum += 1;
                    }else{
                        allWaringProcessedNum += 1;
                    }
                }
                resp.setAllWaringNum(returnAllWaringBeans.size());
                resp.setAllWaringProcessedNum(allWaringProcessedNum);
                resp.setAllWaringUntreatedNum(allWaringUntreatedNum);
            }else{
                resp.setAllWaringNum(0);
                resp.setAllWaringProcessedNum(0);
                resp.setAllWaringUntreatedNum(0);
            }

            if(null != returnAllHikBeans && returnAllHikBeans.size() != 0){
                for(FeedbackBean fb: returnAllHikBeans){
                    if(fb.getPsFeedbackState() == 1){
                        allFaultUntreatedNum += 1;
                    }else{
                        allFaultProcessedNum += 1;
                    }
                }

                resp.setAllFaultNum(returnAllHikBeans.size());
                resp.setAllFaultProcessedNum(allFaultProcessedNum);
                resp.setAllFaultUntreatedNum(allFaultUntreatedNum);
            }else{
                resp.setAllFaultNum(0);
                resp.setAllFaultProcessedNum(0);
                resp.setAllFaultUntreatedNum(0);
            }

            Map map = new HashMap();
            map.put("data",resp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
